<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			 <view class="pricebox">
				 <uni-easyinput v-model="pay_totalamount" class="price" type="digit" trim="true"></uni-easyinput>
			 </view> 
		</view>

		<view class="pay-type-list">

			<view class="type-item b-b" @click="changePayType('wechat')">
				<text class="icon yticon icon-weixinzhifu"></text>
				<view class="con">
					<text class="tit">微信支付</text>
					<text>推荐使用微信支付</text>
				</view>
				<label class="radio">
					<radio value="wechat" color="#fa436a" :checked='pay_type == "wechat"' />
					</radio>
				</label>
			</view>
			<!-- #ifdef APP-PLUS -->
			
			
			<view class="type-item b-b" @click="changePayType('alipay')">
				<text class="icon yticon icon-alipay"></text>
				<view class="con">
					<text class="tit">支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="alipay" color="#fa436a" :checked='pay_type == "alipay"' />
					</radio>
				</label>
			</view>
			<!-- #endif -->  
			
		 
		</view>

		<text class="mix-btn" @click="payit">确认支付</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pay_type: "wechat",
				pay_totalamount: 0,
				pay_channel: 'app', //app,miniapp
				userinfo: 0,
 	 
			};
		},
	
		onLoad(options) {
 
			// #ifdef MP-WEIXIN
			this.pay_channel = "miniapp";
			// #endif
			// #ifdef APP-PLUS
			  this.pay_channel="app";
			// #endif
		 
	 
		},

		methods: {
	 

			//选择支付方式
			changePayType(type) {
				this.pay_type = type;
			},
			//确认支付
			payit: async  function() {
				let params = {
					paychannel: this.pay_channel,
					amount: this.pay_totalamount,
					pay_type: this.pay_type
				};
				let that = this;
				let requrl = "/recharge/createorder";
				 
				 let [errmsg,resdata]= await this.$util.req(requrl, {"data":JSON.stringify(params)}, true);
			     let result = resdata.data;
				 if(result.code !=1){
					 uni.showToast({
					 	title:result.msg
					 });
					 return ;
				 }
				 this.payresult(result.data);
				
			},
			payresult(resdata) {
					let that= this;
					let paysuccess_url= '/pages/money/amountlog';
 
				if (this.pay_channel == "app") { // app 支付，支持微信和支付宝
					let provider = this.pay_type == "wechat" ? 'wxpay' : 'alipay';
					uni.requestPayment({
						provider: provider,
						orderInfo: resdata.paystr,
						success(res) {
						that.$util.toast("支付成功",function(){
							uni.navigateTo({
								url: paysuccess_url
							});
						});
						},
						fail(err) {
						console.log("失败:"+JSON.stringify(err));
						//	that.$util.toast("失败:"+);
						}
					})
				}else if (this.pay_channel == "miniapp") { //微信小程序，只有微信支付
				
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: resdata.paystr.timeStamp,
						nonceStr: resdata.paystr.nonceStr,
						package: resdata.paystr.package,
						signType: resdata.paystr.signType,
						paySign: resdata.paystr.paySign,
						success(res) {
							that.$util.toast("支付成功",function(){
								uni.navigateTo({
									url: paysuccess_url
								});
							});
						},
						fail(err) {
						console.log("失败:"+JSON.stringify(err));
						}
					})
				}

			}
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;
	 
		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 34upx;
            width: 200upx;  
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
